package com.zy.yun.controller.sys;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.yun.base.annotation.NotNullPara;
import com.zy.yun.base.common.DataTable;
import com.zy.yun.base.common.RestResult;
import com.zy.yun.base.constant.StatusEnum;
import com.zy.yun.base.entity.Dict;
import com.zy.yun.base.entity.DictItem;
import com.zy.yun.base.entity.dto.DictDTO;
import com.zy.yun.base.entity.vo.DictVO;
import com.zy.yun.controller.common.BaseController;
import com.zy.yun.provider.service.IDictItemService;
import com.zy.yun.provider.service.IDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 系统字典表 前端控制器
 * </p>
 *
 * @author 王彦委
 * @since 2019-09-26
 */
@Api(tags = "系统字典管理", description = "系统字典管理")
@RestController
@RequestMapping("/dict")
public class DictController extends BaseController<Dict, DictVO> {

    @Autowired
    private IDictService dictService;

    @Autowired
    private IDictItemService dictItemService;
    /**
     * 功能描述: 系统字典分页查询
     *
     * @auther: wangyanwei
     * @date: 2019/9/29 16:15
     * @param:
     * @return:
     */
    @PostMapping("findPage")
    @ApiOperation("系统字典列表")
    public DataTable findPage(DictDTO dictDTO, @RequestParam(defaultValue = "0") Integer pageNum, @RequestParam(defaultValue = "30") Integer pageSize) {

        EntityWrapper<Dict> dictQueryWrapper = new EntityWrapper<>();
        dictQueryWrapper.orderBy("seq");
        if (StringUtils.isNotBlank(dictDTO.getName())) {
            dictQueryWrapper.like("name", dictDTO.getName());
        }
        return findPage(dictQueryWrapper,pageNum,pageSize);

    }

    /**
     * 功能描述: 根据id,批量删除
     *
     * @auther: wangyanwei
     * @date: 2019/9/30 9:34
     * @param:
     * @return:
     */

    @ApiOperation("根据id,批量删除")
    @PostMapping("dictbatchDelete")
    @ApiImplicitParam(value = "ids", name = "记录id", required = true)
    public RestResult DictbatchDelete(@RequestParam("ids") List<Long> ids) {

        if (CollectionUtils.isEmpty(ids)) {
            return RestResult.buildError("请选择要删除的记录！");
        }
        //判断其下是否还有子项目
        EntityWrapper<DictItem> dictItemQueryWrapper = new EntityWrapper<>();
        dictItemQueryWrapper.in("dictId",ids);
        List<DictItem> dictItemList = dictItemService.selectList(dictItemQueryWrapper);
        if(CollectionUtils.isNotEmpty(dictItemList)){
            return RestResult.buildError("被删除字典下面有子项目！");
        }
        if(dictService.deleteBatchIds(ids)){
            return RestResult.buildSuccess();
        }
        return RestResult.buildError();
    }

    /**
     * 功能描述: 新增
     *
     * @auther: wangyanwei
     * @date: 2019/9/24 9:28
     * @param:
     * @return:
     */
    @PostMapping("save")
    @ApiOperation(value = "新增记录")
    @NotNullPara({"name","value"})
    public RestResult save(DictDTO dictDTO) {

        if (StringUtils.isNotBlank(dictDTO.getNote()) && dictDTO.getNote().length()>50) {
            return RestResult.buildError("备注最多50个字！");
        }

        //判断字典名称是否存在
        EntityWrapper<Dict> dictItemQueryWrapper = new EntityWrapper<>();
        dictItemQueryWrapper.eq("name", dictDTO.getName());
        List<Dict> list = dictService.selectList(dictItemQueryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            return RestResult.buildError("名称已经存在！");
        }

        dictDTO.setStatus(StatusEnum.STATUS_START.getCode());
        if (dictService.insert(dictDTO)) {
            return RestResult.buildSuccess();
        }
        return RestResult.buildError();
    }


}
